package com.example.swordjava;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;

public class Test7 {

    public static void main(String[] args) {

        int[] a = {2, 3, 1, 0, 2, 5, 3};

    }

    public static class TreeNode {
        int val;
        TreeNode left = null;
        TreeNode right = null;

        TreeNode(int val) {
            this.val = val;
        }
    }

    /**
     * 给定节点数为 n 的二叉树的前序遍历和中序遍历结果，请重建出该二叉树并返回它的头结点。
     * 例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}，
     */

    public TreeNode reConstructBinaryTree (int[] preOrder, int[] vinOrder) {
        // write code here
        if (preOrder.length==0)return null;
        TreeNode root = null;

        for (int i = 0; i < vinOrder.length; i++) {
            if (preOrder[0]==vinOrder[i]){
                root = new TreeNode(preOrder[0]);
                root.left = reConstructBinaryTree(Arrays.copyOfRange(preOrder,1,i+1),Arrays.copyOfRange(vinOrder,0,i));
                root.right = reConstructBinaryTree(Arrays.copyOfRange(preOrder,i+1,preOrder.length),Arrays.copyOfRange(vinOrder,i+1,vinOrder.length));
            }
        }

        return root;
    }

}
